package com.cloudbroker.bcs.csvc.push.facade.util;


import cn.jpush.api.JPushClient;

/**
 * JPushClient 工具类
 * @author zxpan
 *
 */
public class ClientConstant {
	
	private static int maxRetryTimes = 1;

	/**
	 * 根据接入渠道号生成JPushClient
	 * @return
	 */
	public static JPushClient getClient(String appKey, String mastersecret) {
		JPushClient client = new JPushClient(mastersecret, appKey, maxRetryTimes);
		return client;
	}

	/**
	 * 根据接入渠道号生成JPushClient
	 * @param senderCompId
	 * @return
	 */
	public static JPushClient getClient(String sendercomPids, String mastersecret, 
			String appKey, String senderCompId) {
		String jPushKey;
		String appCesert;
		
		// 获取接入渠道号对应配置中下标
		String[] compList = sendercomPids.split(",");
		for(int i = 0; i < compList.length; i ++) {
			String comp = compList[i];
			
			if (comp.equals(senderCompId)) {
				
				// 获取JPush appKey
				jPushKey = getAppKey(appKey, i);
				appCesert = getMastersecert(mastersecret, i);
				JPushClient client = new JPushClient(appCesert, jPushKey, maxRetryTimes);
				if(client != null){
					
					return client;
				}
			}
		}
		System.out.println("******消息推送：接入渠道号不正确，未找到匹配的APPKEY，使用默认[20080]******");
		return new JPushClient(getMastersecert(mastersecret,0), getAppKey(appKey,0), maxRetryTimes);
	}

    /**
     * 获取极光APPKEY
     * @param op 下标
     * @return
     */
    private static String getAppKey(String appKey, int op){
    	String[] keyList = appKey.split(",");
    	if(keyList != null && keyList.length > 0) {
    		return keyList[op];
    	}
    	return "";
    }
    
    /**
     * 
     * 获取极光MASTERSECRET
     * @param op 下标
     * @return
     */
    private static String getMastersecert(String mastersecret, int op) {
    	String[] secertList = mastersecret.split(",");
    	if(secertList != null && secertList.length > 0){
    		return secertList[op];
    	}
    	return "";
    }
}
